package com.xz.xzutils.settings.gui;

import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.TextFieldWithBrowseButton;
import com.intellij.util.xmlb.XmlSerializerUtil;
import com.xz.xzutils.settings.mybatislogplus.MybatisLogPlusState;
import com.xz.xzutils.settings.mybatislogplus.constant.FormatterEnum;
import com.xz.xzutils.settings.mybatislogplus.constant.SqlPrintStyleEnum;
import lombok.Getter;
import lombok.Setter;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.*;

/**
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025/3/1-03-01 13:53
 */
public class MybatisLogPlusSettings {
    private JPanel mybatisLogPlusRoot;
    private JRadioButton compressed;
    private JRadioButton formatted;
    private JRadioButton raw;
    private JCheckBox saveLogFile;
    private JPanel logFilePathPanel;
    private JSpinner indentNum;
    private TextFieldWithBrowseButton logFilePath;
    private JPanel indentPanel;
    private JSpinner minPort;
    private JSpinner maxPort;
    private ComboBox<FormatterEnum> formatter;
    private ButtonGroup buttonGroup;

    @Getter
    @Setter
    private MybatisLogPlusState uiState = new MybatisLogPlusState();

    public JPanel getRootComponent() {
        return mybatisLogPlusRoot;
    }

    private void setShowIndentPanel(boolean show) {
        if (show) {
            indentPanel.setVisible(true);
            indentPanel.setEnabled(true);
            indentNum.setValue(uiState.getIndentNum());
        } else {
            indentPanel.setVisible(false);
            indentPanel.setEnabled(false);
        }
    }

    // SQL日志
    private void initSaveLogFile() {
        saveLogFile.setSelected(uiState.getSaveLogToFile());
        logFilePath.setText(uiState.getLogFilePath());
        // TODO 保存日志文件
        logFilePath.setEnabled(saveLogFile.isSelected());
        logFilePath.setEditable(saveLogFile.isSelected());
        saveLogFile.addChangeListener(e -> {
            logFilePath.setEnabled(saveLogFile.isSelected());
            logFilePath.setEditable(saveLogFile.isSelected());
            uiState.setSaveLogToFile(saveLogFile.isSelected());
        });
        // 文件路径事件
    }

    // SQL输出格式
    private void initSqlLogPrintStyle() {
        // 格式化库
        formatter.setModel(new DefaultComboBoxModel<>(FormatterEnum.values()));
        formatter.setSelectedItem(uiState.getFormatter());
        formatter.addActionListener(e -> {
            uiState.setFormatter(formatter.getItem());
        });
        // 打印格式
        ItemListener itemListener = e -> {
            if (e.getStateChange() == ItemEvent.SELECTED) {
                JRadioButton selected = (JRadioButton) e.getSource();
                // 显示缩进
                this.setShowIndentPanel(formatted.isSelected());
                if (selected.getActionCommand().equals(compressed.getActionCommand())) {
                    uiState.setSqlPrintStyle(SqlPrintStyleEnum.COMPRESSED);
                } else if (selected.getActionCommand().equals(formatted.getActionCommand())) {
                    uiState.setSqlPrintStyle(SqlPrintStyleEnum.FORMATTED);
                } else if (selected.getActionCommand().equals(raw.getActionCommand())) {
                    uiState.setSqlPrintStyle(SqlPrintStyleEnum.RAW);
                }
            }
        };
        compressed.addItemListener(itemListener);
        formatted.addItemListener(itemListener);
        raw.addItemListener(itemListener);
        // 初始化选中
        switch (uiState.getSqlPrintStyle()) {
            case RAW -> raw.setSelected(true);
            case FORMATTED -> formatted.setSelected(true);
            case COMPRESSED -> compressed.setSelected(true);
        }
        // 初始化缩进
        indentNum.addChangeListener(e -> {
            uiState.setIndentNum((Integer) indentNum.getValue());
        });
        this.setShowIndentPanel(formatted.isSelected());
    }

    // 端口设置
    private void initPort() {
        minPort.setValue(uiState.getMinPort());
        maxPort.setValue(uiState.getMaxPort());
        minPort.addChangeListener(e -> {
            uiState.setMinPort((Integer) minPort.getValue());
        });
        maxPort.addChangeListener(e -> {
            uiState.setMaxPort((Integer) maxPort.getValue());
        });
    }

    public void renderUI(MybatisLogPlusState resetState) {
        XmlSerializerUtil.copyBean(resetState, this.uiState);
        this.initSaveLogFile();
        this.initSqlLogPrintStyle();
        this.initPort();
    }
}
